package com.le.tester.javaThink.fourth.concurrency;

import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * createTime：2021/11/5 17:02
 * description：关闭资源
 */
public class CloseResource {
    public static void main(String[] args) throws Exception {
        ExecutorService service = Executors.newCachedThreadPool();
        ServerSocket socket = new ServerSocket(9300);
        InputStream in = new Socket("127.0.0.1", 9300).getInputStream();

        service.execute(new IOBlocked(in));
        service.execute(new IOBlocked(System.in));

        TimeUnit.MILLISECONDS.sleep(100);

        System.out.println("Shutting down all threads");
        service.shutdownNow();

        TimeUnit.SECONDS.sleep(1);

        System.out.println("Closing " + in.getClass().getName());
        in.close();
        TimeUnit.SECONDS.sleep(1);
        System.out.println("Closing " + System.in.getClass().getName());
        System.in.close();
    }
}
